﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using TimKen.DataAccess;
using Common;
using TimKen.DataEntity;
using System.Data.SqlClient;

namespace TimKen.Web.Ashx
{
    /// <summary>
    /// DealerDetailHandler 的摘要说明
    /// </summary>
    public class DealerDetailHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string provinceID = context.Request.Params["provinceID"];
            string action = context.Request.Params["action"];
            AjaxResponseInfo info = new AjaxResponseInfo { IsSuccess = false, Msg = "请求失败!" };
            switch (action)
            {
                case "DealerName":
                    if (!string.IsNullOrEmpty(provinceID))
                    {
                        info.IsSuccess = true;
                        info.Data = JsonHelper.ObjectToJson(BindDealer(provinceID.Replace("p", "")));
                    }
                    break;
                case "LoadData":
                    info.IsSuccess = true;
                    info.Data = JsonHelper.ObjectToJson(BindDealer(provinceID.Replace("p", "")));
                    break;
                default:
                    break;
            }
            context.Response.Write(JsonHelper.ObjectToJson(info));
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        private List<DealerInfo> BindDealer(string provinceID)
        {
            string sql = String.Format("select * from View_Dealer where provinceID ={0} order by ProvinceID ", provinceID);
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);
            List<DealerInfo> list = new List<DealerInfo>();
            while (dr.Read())
            {
                DealerInfo info = new DealerInfo
                {
                    
                    ProvinceID = dr["ProvinceID"].ToString(),
                    dealerName = dr["dealerName"].ToString()
                };
                list.Add(info);
            }
            return list;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}